home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / doors / forms / formedit.c next >
Encoding:
C/C++ Source or Header  |  1992-12-26  |  13.1 KB  |  548 lines

  1. /*************************************************************************
  2.                 FORMS version 1.0 written by Joseph Hodge.
  3.                 ------------------------------------------
  4.  
  5.  This AmiExpress Interface allows you to provide templates as text files
  6.  which will allow users to fill the templates out via a FullScreen
  7.  editing capability.
  8. **************************************************************************/
  9.  
  10. #include <exec/exec.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "dh1:source/doors/romconf/doorheader.h"
  14. #define ACCESS_WRITE -1
  15.  
  16. /****** AmiExpress now will return arrow key information as the following
  17.         codes, these codes are stored as the first character in any
  18.         getuserstring function
  19.  ******/
  20.  
  21.  
  22. #define RIGHTARROW 2
  23. #define LEFTARROW 3
  24. #define UPARROW 4
  25. #define DOWNARROW 5
  26.  
  27. #define gu getuserstring
  28.  
  29. char cmd[100]; /* misc storage throughout program */
  30. char im[200][82]; /* storage for text user types and also the 15 line forms */           
  31.  
  32. int x,y,line; /* holds current cursor coordinates & max number of lines */
  33.  
  34. void end(); /** Required for any interface for AmiExpress. **/
  35. void showstat(int line,int x,int y); /** Places cursor in appropriate 
  36.                                          location **/
  37.  
  38. void InitArray(); /* Initializes im[][] to spaces */
  39. void SaveAs(); /* saves finished form on drive */
  40. void showarg(char *str[]); /* This will show all of the command line
  41.                               arguments used to invoke this interface
  42.                            */
  43.  
  44. void makearg(char *str); /* parses the commandline and stores the arg
  45.                             pointers in argment[]
  46.                          */
  47.  
  48. void sr(char *str); /* removes trailing ascii codes < 33 from any string */
  49.  
  50. void LoadForm(char *str); /* loads the desired Form Template */
  51.  
  52. void Goto(int x,int y); /* Used to position cursor at specific locations
  53.                            on the screen
  54.                         */
  55. int node; /* contains the node number from where this interface was invoked */
  56.  
  57. void LoadFormsPath(char *str);
  58. int UserFound(void);
  59. void UserSave(void);
  60.  
  61. #define sm sendmessage
  62. #define cm sendchar
  63. #define pu putuserstring
  64. #define co ConOnly
  65. #define fk Fhotkey
  66. BOOL FORM=FALSE; /* set to true if there is a form for the editor */
  67.  
  68. char *argment[50];              /** Pointers to commandline arguments **/
  69.  
  70. int argcount=0;                 /** Total number of commandline arguments **/
  71.  
  72. struct FORMS                    /** Holds location & response for enteries **/
  73. {
  74.   short x,y;
  75.   char content[80];
  76. } Fm[100];
  77.  
  78. int LastForm=0; /* contains total number of entries allowed on the form
  79.                    based on '[' in template */
  80.  
  81. int CurrForm=0; /* contains the current entry the user is editing. */
  82. char FORMPATH[200];
  83. BOOL APPEND;
  84. BOOL UNIQUE;
  85. BOOL NODEPREFIX;
  86. long lock=0L; 
  87. main(int argc,char *argv[])
  88. {
  89.   char hmd;
  90.   char temp[10];
  91.   int t=0;
  92.   BOOL stop=FALSE; /* provides a loop for the editor */
  93.   int tline=0,i;   /* tline holds current line the user it on */
  94.   x=y=line=0;
  95.  
  96.   if(argc!=2)
  97.   {
  98.      printf("Forms v1.0 written by Joseph Hodge\n");
  99.      printf("This is an XIM for AmiExpress v2.0+\n");
  100.      printf("\n");
  101.      exit(0);
  102.   }
  103.   Register(argv[1][0]-'0'); /** Initiliaze communication between BBS
  104.                                 and program
  105.                              **/
  106.  
  107.   node = argv[1][0]-'0'; /** store current node the user is on **/
  108.   pu("",RAWARROW); /** Turns off arrowkey filter, allowing us to
  109.                          intercept them and do what we wish with it
  110.                       **/
  111.   InitArray();
  112.  
  113.   
  114.  
  115.   gu(cmd,BB_MAINLINE); /* determine commandline used to invoke the FORMS */
  116.   makearg(cmd); /* parse the commandline */
  117.   
  118. if(argcount==1)
  119.     LoadForm(argment[0]);
  120.  else LoadForm(NULL);
  121.   Goto(Fm[CurrForm].x,Fm[CurrForm].y); x=Fm[CurrForm].x;
  122.   y=Fm[CurrForm].y;
  123.  
  124.   /** loop until the user presses '/' on the begining of any line **/
  125.   do
  126.   {
  127.      hotkey("",temp);hmd=temp[0];
  128.      //hmd=Fhotkey();
  129.      switch(hmd)
  130.      {
  131.        case RIGHTARROW:
  132.             if(FORM)
  133.             {
  134.                if(x-1>=0)
  135.                {
  136.                  if(im[y][x-1]=='[')
  137.                  {
  138.                    CurrForm -=1; if(CurrForm<0) CurrForm=LastForm-1;
  139.                    y=Fm[CurrForm].y;x=Fm[CurrForm].x;Goto(x,y);
  140.                  }
  141.                  else
  142.                  {
  143.                    x -=1; if(x<0) x=0; else sm("D",0);
  144.                 
  145.                  }
  146.  
  147.                }
  148.              }
  149.              else
  150.              {
  151.                 x -=1; if(x<0) x=0; else sm("D",0);
  152.                 
  153.              }
  154.              break;
  155.        case LEFTARROW:
  156.             if(FORM)
  157.             {
  158.                if(x+1<=79)
  159.                {
  160.                  if(im[y][x+1]==']')
  161.                  {
  162.                    CurrForm +=1; if(CurrForm>=LastForm) CurrForm=0;
  163.                    y=Fm[CurrForm].y;x=Fm[CurrForm].x;Goto(x,y);
  164.                  }
  165.                  else 
  166.                  {
  167.                      x +=1; if(x>79) x=79; else sm("C",0); 
  168.                  }
  169.                }
  170.              }
  171.              else
  172.              {
  173.               x +=1; if(x>78) x=78; else sm("C",0); 
  174.              }
  175.              break;
  176.        case UPARROW:
  177.             if(FORM)
  178.             {
  179.                        CurrForm -=1; if(CurrForm<0) CurrForm=LastForm-1;
  180.                        y=Fm[CurrForm].y;x=Fm[CurrForm].x; Goto(x,y); 
  181.             }else
  182.             {
  183.               y -=1; if(y<0) y=0; else { sm("A",0); tline--; }
  184.             }
  185.             break;
  186.        case DOWNARROW:
  187.             if(FORM)
  188.             {
  189.               CurrForm +=1; if(CurrForm>=LastForm) CurrForm=0;
  190.                        y=Fm[CurrForm].y;x=Fm[CurrForm].x; Goto(x,y);  
  191.             }else
  192.             {         
  193.               y +=1; if(y>16) y=16; else{ sm("B",0); tline++;
  194.                    if(tline>line) line++;} 
  195.             }
  196.             break;
  197.        case 12:
  198.        case 13:
  199.             if(FORM)
  200.             {
  201.                 CurrForm +=1; if(CurrForm>=LastForm) CurrForm=0;
  202.                 y=Fm[CurrForm].y; x=Fm[CurrForm].x; Goto(x,y);
  203.             }
  204.             else
  205.             {
  206.                 y +=1; x=0; if(y>16) y=16; else { tline++;if(tline>line)line++;}
  207.                
  208.             }
  209.             break;
  210.        case '\b':
  211.             if(FORM)
  212.             {
  213.                if(x-1>=0)
  214.                {
  215.                  if(im[y][x-1]=='[')
  216.                  {
  217.                    CurrForm -=1; if(CurrForm<0) CurrForm=LastForm-1;
  218.                    y=Fm[CurrForm].y;x=Fm[CurrForm].x;Goto(x,y);
  219.                  }
  220.                  else
  221.                  {
  222.                    x -=1; if(x<0) x=0; else sm("\b \b",0);
  223.                 
  224.                  }
  225.  
  226.                }
  227.              }
  228.              else {
  229.             x = x-1; if(x<0) x=0; else sm("\b \b",0);} break;
  230.        case '\t':
  231.               CurrForm +=1; if(CurrForm>=LastForm) CurrForm=0;
  232.                 y=Fm[CurrForm].y; x=Fm[CurrForm].x; Goto(x,y);
  233.               break;
  234.        default: if((hmd=='S' || hmd=='A' || hmd=='a' || hmd=='s')&& CurrForm==LastForm-1) { stop=TRUE; break;} 
  235.             cm(hmd,0); im[y][x]=hmd; 
  236.             if(FORM)
  237.             {
  238.               if(x+1<=79)
  239.                {
  240.                  if(im[y][x+1]==']')
  241.                  {
  242.                    CurrForm +=1; if(CurrForm>=LastForm) CurrForm=0;
  243.                    y=Fm[CurrForm].y;x=Fm[CurrForm].x;Goto(x,y);
  244.                  }
  245.                  else 
  246.                  {
  247.                      x +=1; if(x>79) x=79; //else sm("C",0); 
  248.                  }
  249.                }
  250.             }
  251.             else { x +=1; if(x>78)
  252.                  {
  253.                    if(y+1<=16)
  254.                    {
  255.                      x=78; while(x>=0 && im[y][x]!=' ') x--;
  256.                       x++; t=x; x=0; while(t<=78)
  257.                      {
  258.                        im[y+1][x]=im[y][t];im[y][t]=' ';
  259.                        Goto(t,y);cm(im[y][t],0);Goto(x,y+1);cm(im[y+1][x],0); 
  260.                        x++; t++;
  261.                      }
  262.                      y++;
  263.                    }
  264.                    else x=78;
  265.                  }
  266.                 }
  267.      }
  268.    //  co(" p",0);showstat(line,x,y); co(" p",0);
  269.   }while(!stop);
  270.   
  271.   if(hmd=='S' || hmd=='s')SaveAs();
  272.   sm(" ",0);
  273.   sm("",1);
  274.   sm("Exiting FullEdit",1);
  275.   sm("",1);
  276.   ShutDown();
  277.   end();
  278. }
  279. void InitArray()
  280. {
  281.   register int i,j;
  282.   for(i=0;i<200;i++)
  283.   {
  284.     for(j=0;j<79;j++)
  285.      im[i][j]=' '; im[i][j]='\0';
  286.   }
  287. }
  288. void showstat(int line,int x,int y)
  289. {
  290.   char str[200];
  291. /*    sm("",0);
  292.     sm("H",0);
  293.  
  294.  
  295. sprintf(str,"--%3d--%2d--%2d--------------------------------------------------------------",line,x,y);
  296.     sm(str,1);
  297. */
  298.     sprintf(str,"%d;%dH",y+3,x+1);
  299.     sm(str,0);
  300. }
  301. void SaveAs()
  302. {
  303.   FILE *fi;
  304.   char FileName[200];
  305.   register int i;
  306.   sprintf(FileName,"%s%s.frm",FORMPATH,(STRPTR)argment[0]);
  307.   while(!(lock=Lock(FileName,ACCESS_WRITE)));
  308.   
  309.   sprintf(FileName,"%s%s.dat",FORMPATH,(STRPTR)argment[0]);
  310.   fi=fopen(FileName,"a");
  311.   if(fi==NULL)
  312.   {
  313.     sm("Error, cannot locate node(x)/work",1);
  314.     UnLock(lock);
  315.     return;
  316.   }
  317.   i=0;
  318.   while(i<line)
  319.   {
  320.     fprintf(fi,"%s\n",im[i]);
  321.     i++;
  322.   }
  323.   fclose(fi);
  324.   UnLock(lock);
  325.   if(UNIQUE)
  326.   {
  327.     UserSave();
  328.   }
  329. }
  330. void end()
  331. {
  332.   exit(0);
  333. }
  334. void showarg(char *str[])
  335. {
  336.   register int i=0;
  337.   while(str[i][0]!=NULL)
  338.   {
  339.     strcpy(cmd,str[i]);
  340.     sm(cmd,1);
  341.     i++;
  342.   }
  343. }
  344. void makearg(char *str)
  345. {
  346.   static int i=0;
  347.   static int j=0;
  348.   argcount=0;
  349. while(1)
  350. {
  351.   argment[j]=str+i; j++; argment[j]=NULL; argcount++;
  352.   if(*(str+i)=='\0') { argment[j]=NULL; return; }
  353.   while(*(str+i)>32)
  354.   {
  355.     i++;
  356.   }
  357.   if(*(str+i)=='\0') return;
  358.   *(str+i)='\0';i++;
  359.  }
  360. }
  361.  
  362. void LoadForm(char *str)
  363. {
  364.   FILE *fi;
  365.   char local[100];
  366.   char FileName[100];
  367.   char showname[200];
  368.   char cmd[200];
  369.   register int i=0,j;
  370.   if(str!=NULL)
  371.   {
  372.     strcpy(FileName,str);
  373.     LoadFormsPath(str);
  374.     sprintf(FileName,"%s%s.frm",FORMPATH,str);
  375.     sprintf(showname,"%s%s",FORMPATH,str);
  376.     if(UserFound()) 
  377.     {
  378.       //sm("Records indicate you have already filled out a form",1);
  379.       ShutDown();
  380.       end();
  381.     }
  382. // co(" p",0); /* turn console cursor off */
  383. //  showstat(line,x,y); /* move cursor to correct place */
  384. //  co(" p",0);  /* turn console cursor back on */
  385.  
  386.   sm(" ",0);/* clears the screen */
  387.  
  388.   sm("FORMS version 1.0  written by Joseph Hodge",1);
  389. sm("------------------------------------------------------------------------------",1);
  390.  
  391.   sm("1H",0);
  392.   sm("",0);
  393.  
  394. sm("------------------------------------------------------------------------------",1);
  395. sm("Press S)ave A)bort [ ].",1);
  396. sm("------------------------------------------------------------------------------",0);
  397.  
  398.     fi=fopen(FileName,"r");
  399.     if(fi==NULL) return;FORM=TRUE;
  400.     while(fgets(cmd,80,fi)!=NULL && i<15)
  401.    {
  402.    
  403.     j=0; 
  404.     sprintf(local,"%d;%dH",i+3,1);
  405.     sm(local,0);
  406.     sr(cmd);
  407.     while(cmd[j]!='\0' && j<79)
  408.     {
  409.       im[i][j]=cmd[j]; if(cmd[j]=='[') { Fm[LastForm].x=j+1;
  410.                                          Fm[LastForm].y=i; LastForm++; }
  411.       j++;
  412.     }
  413.     //sm(cmd,0);
  414.     i++; line++;
  415.   }
  416.   }
  417.   Goto(1,0);
  418.   if(str==NULL)
  419.   {
  420.     Fm[LastForm].x=0 ;
  421.     Fm[LastForm].y=0 ;
  422.     LastForm++;
  423.   }
  424.   Fm[LastForm].x=20;
  425.   Fm[LastForm].y=18;
  426.   im[18][19]='[';
  427.   im[18][21]=']';
  428.   LastForm++;
  429.   
  430.   
  431.   if(fi!=NULL){fclose(fi); showgfile(showname);}
  432. }
  433.  
  434. void sr(char *str)
  435. {
  436.   register int i;
  437.   i=strlen(str)-1;
  438.   while(i>-1)
  439.   {
  440.     if(*(str+i)<=32) *(str+i)='\0'; else break;
  441.     i--;
  442.   }
  443. }
  444.  
  445. void Goto(int x,int y)
  446. {
  447.   char local[20];
  448.   sprintf(local,"%d;%dH",y+3,x+1);
  449.   sm(local,0);
  450. }
  451. void LoadFormsPath(char *str)
  452. {
  453.   FILE *fi;
  454.   char cmd[200];
  455.   APPEND=FALSE;
  456.   UNIQUE=FALSE;
  457.   NODEPREFIX=FALSE;
  458.   strcpy(FORMPATH,"DOORS:FormEdit/Forms/");
  459.   sprintf(cmd,"%s%s.cfg",FORMPATH,(STRPTR)argment[0]);
  460.   fi=fopen(cmd,"r");
  461.   if(fi==NULL) return;
  462.   while(fgets(cmd,80,fi)!=NULL)
  463.   {
  464.     sr(cmd);strupr(cmd);
  465.     if(!strcmp(cmd,"NODE PREFIX"))
  466.     {
  467.       NODEPREFIX=TRUE;
  468.     }
  469.     if(!strcmp(cmd,"UNIQUE RECORD"))
  470.     {
  471.       UNIQUE=TRUE;
  472.     }
  473.     if(!strnicmp(cmd,"PATH",4))
  474.     {
  475.       strcpy(FORMPATH,&cmd[5]);
  476.     }
  477.     if(!strcmp(cmd,"APPEND"))
  478.     {
  479.       APPEND=TRUE;
  480.     }
  481.     
  482.   }
  483.   fclose(fi);
  484. }
  485. int UserFound(void)
  486. {
  487.   FILE *fi;
  488.   char Name[200];
  489.   char FileName[200];
  490.   gu(Name,DT_NAME);
  491.   sprintf(FileName,"%s%s.db",FORMPATH,argment[0]);
  492.   strupr(Name);
  493.   if(!UNIQUE) return(0);
  494.   fi=fopen(FileName,"r");
  495.   if(fi==NULL)
  496.   {
  497.     return(0);
  498.   }
  499.   while(fgets(FileName,80,fi)!=NULL)
  500.   {
  501.     sr(FileName); 
  502.     if(!strcmp(Name,FileName))
  503.     {
  504.      fclose(fi);return(1);
  505.     }
  506.   }
  507.   fclose(fi);
  508.   return(0);
  509. }
  510. void UserSave(void)
  511. {
  512.   FILE *fi;
  513.   char Name[200];
  514.   char FileName[200];
  515.   gu(Name,DT_NAME);
  516.   sprintf(FileName,"%s%s.frm",FORMPATH,argment[0]);
  517.   while(!(lock=Lock(FileName,ACCESS_WRITE)));
  518.   sprintf(FileName,"%s%s.db",FORMPATH,argment[0]);
  519.   strupr(Name);
  520.   if(!UNIQUE) return;
  521.   fi=fopen(FileName,"a");
  522.   if(fi==NULL)
  523.   {
  524.     UnLock(lock);
  525.     return;
  526.   }
  527.   fprintf(fi,"%s\n",Name);
  528.   fclose(fi);
  529.   UnLock(lock);
  530. }
  531.  
  532. int strnicmp(char *str1,char *str2,int len)
  533. {
  534.   register int i;
  535.   strupr(str1);
  536.   strupr(str2);
  537.   i=0;
  538.   while(i<len)
  539.   {
  540.     if(*(str1+i)!=*(str2+i)) return(1);
  541.     i++;
  542.   }
  543.   return(0);
  544. }
  545. void LastCommand(void)
  546. {
  547. }
  548.